#pragma once
#include "streambuff_interface.h"

#ifdef WIN32
#include <winsock2.h>
#include <process.h>
#include <windows.h>
#else // using pthread
#include <pthread.h>
#include <semaphore.h>
#endif // WIN32

class CSteamBuffer
{
public:
	CSteamBuffer(void);
	~CSteamBuffer(void);
	///////////////////////
	char *			pIndex;
	unsigned int	nPosCount;
	int				nWritePos;
	int				nMinReadPos;

	unsigned int	nMinPosInterval;

	/////////
	char *			pDataBuf;

	char *			pDataBufStart;
	char *			pDataBufEnd;
	
	unsigned int	nDataBufSize;
	unsigned int	nMaxOneDataSize;

	char *			pDataWriteStar;
	char *			pDataWriteEnd;

	unsigned int	nMinDataInterval;
	///////////
	int				nStreamBufferNumber;
	int				m_nNeedLock;

#ifdef WIN32
	CRITICAL_SECTION mutex;
#else // pthread mutex
	pthread_mutex_t mutex;
#endif // 
	/////////

	//////////////////////
	int init(int nStreamNumber,unsigned int nBufferSize,unsigned int nIndexCount,unsigned int nFrameInterval,unsigned int nNeedLock);
	int close(void);
	int StreamWrite(SFStream_Data_Info DataInfo);
	int StreamGetLastIFrame(int* pReadCurPos, unsigned int startTime);
	int SFStreamRead(SFStream_Data_Info* DataInfo, int* pReadCurPos);

	inline void ChangeMinReadPos(int nStarPost);
};

